<template>
  <div class="containerHall">
    <el-dialog
      :title="dialogConfig.name"
      :visible.sync="dialogVisible"
      @close="dialogVisible = false"
      append-to-body
    >
      <el-form ref="formRef" :model="formData" :rules="rules">
        <el-form-item label="标题" :label-width="'100px'" prop="uid">
          <el-input v-model="formData.title" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="通知内容" :label-width="'100px'" prop="seqencing">
          <el-input
            v-model="formData.content"
            type="textarea"
            :autosize="{ minRows: 5 }"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="推送说明" :label-width="'100px'" prop="link">
          <el-input v-model="formData.remark" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="通知范围" :label-width="'100px'" prop="seqencing">
          <el-radio-group v-model="formData.type">
            <el-radio :label="1">全部用户</el-radio>
            <el-radio :label="2">指定id</el-radio>
          </el-radio-group>
          <el-input
            v-if="formData.type == 2"
            v-model="formData.uids"
            type="textarea"
            :autosize="{ minRows: 2 }"
            placeholder="请输入uid(输入多个uid请回车换行，一行一个uid)"
          >
          </el-input>
        </el-form-item>

        <el-form-item label="跳转链接" :label-width="'100px'" prop="seqencing">
          <el-input v-model="formData.link" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="发送类型" :label-width="'100px'" prop="seqencing">
          <el-select
            v-model="formData.sendType"
            placeholder="请选择"
            :disabled="dialogConfig.name == '编辑'"
          >
            <el-option
              v-for="item in sendTypeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item
          label="定时发送时间"
          :label-width="'100px'"
          prop="seqencing"
          v-if="formData.sendType == 2"
        >
          <el-date-picker
            v-model="formData.sendTime"
            type="datetime"
            placeholder="选择日期时间"
            value-format="timestamp"
            :disabled="dialogConfig.name == '编辑'"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleConfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { deepClone } from '@/utils'
import { pushUpdateApi, pushAddApi } from '@api/whisper'
export default {
  components: {},
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    value: {
      type: Object
    },
    request: {
      type: Object
    },
    dialogConfig: {
      type: Object
    }
  },
  data() {
    return {
      posting: false,
      formData: this.setup(this.value),
      rules: {},
      sendTypeOptions: [
        {
          value: 1,
          label: '立即发送'
        },
        {
          value: 2,
          label: '定时发送'
        }
      ]
    }
  },
  created() {},
  mounted() {},
  methods: {
    setup() {
      if (!this.value) {
        return {
          id: '',
          title: '',
          content: '',
          link: '',
          remark: '',
          sendTime: '',
          sendType: 1,
          type: 1,
          uids: ''
        }
      } else {
        const clone = deepClone(this.value)
        return clone
      }
    },
    handleConfirm() {
      this.posting = true
      let func = null
      if (this.formData.id) {
        func = pushUpdateApi
      } else {
        func = pushAddApi
      }
      const clone = deepClone(this.formData)
      if (clone.uids) {
        clone.uids = this.disposeContentToHtml(clone.uids)
      }
      func(clone)
        .then(res => {
          this.dialogVisible = false
          this.$emit('success') // 刷新
        })
        .catch(err => {
          this.$message('error')
        })
    },

    disposeContentToHtml(val) {
      const reg = new RegExp('\n', 'g')
      let text = val.replace(reg, ',')
      return text.replace(/\s/g, '')
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.formData = this.setup()
      }
    }
  },
  computed: {
    dialogVisible: {
      get() {
        return this.visible
      },
      set(val) {
        this.$emit('update:visible')
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.containerHall {
}
</style>
